﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.DataAccess;
using FB.Utilities;

namespace FB.Bussiness
{
    public class Video_TagBO
    {
        public IEnumerable<Video_Tag> GetAllVideo_Tag()
        {
            Video_TagDAO _dao = new Video_TagDAO();
            return _dao.GetAllVideo_Tag();
        }
        public Video_Tag GetVideo_TagById(Guid id)
        {
            Video_TagDAO _dao = new Video_TagDAO();
            return _dao.GetVideo_TagByID(id);
        }
        public bool UpdateVideo_Tag(Video_Tag inVideo_Tag)
        {
            Video_TagDAO _dao = new Video_TagDAO();
            return _dao.UpdateVideo_Tag(inVideo_Tag);
        }
        public bool InsertVideo_Tag(Video_Tag inVideo_Tag)
        {
            Video_TagDAO _dao = new Video_TagDAO();
            return _dao.InsertVideo_Tag(inVideo_Tag);
        }

        public ReturnValue<bool> DelVideo_Tag(Guid id)
        {
            Video_TagDAO _dao = new Video_TagDAO();
            return _dao.DelVideo_Tag(id);
        }

        public List<Guid> GetCategoryIDOfVideo(Guid videoID)
        {
            List<Guid> result = new List<Guid>();
            Video_TagDAO _dao = new Video_TagDAO();
            IEnumerable<Video_Tag> listTag = _dao.GetTagIDOfVideo(videoID).ToList();
            foreach (Video_Tag item in listTag)
            {
                result.Add(item.IDTag);
            }
            return result;
        }


        public void UpdateCategoryOfVideo(Guid videoID, List<Guid> videoCatList)
        {
            // Delete all category of this Video
            Video_TagDAO _dao = new Video_TagDAO();
            _dao.DeleteVideo_TagByVideoID(videoID);
            // Add new category for this Video
            foreach (Guid tag in videoCatList)
            {
                Video_Tag newVideo_Tag = new Video_Tag();
                newVideo_Tag.IDTag = tag;
                newVideo_Tag.IDVideo = videoID;
                _dao.InsertVideo_Tag(newVideo_Tag);
            }
        }

        public void UpdateTagOfVideo(Guid videoID, string tagString)
        {
            Video_TagDAO ReDAO = new Video_TagDAO();
            // Delete all Video_Tag for this video
            ReDAO.DeleteVideo_TagByVideoID(videoID);


            // Split string to
            string[] Tags = tagString.Split(';');

            // Insert Tag if not exist
            // Insert Video_Tag for this video
            foreach (string tag in Tags)
            {
                if (tag != string.Empty)
                {
                    Guid? TagID = InsertTagIfNotExist(tag);
                    InsertVideo_Tag(videoID, TagID);
                }
            }
        }

        private void InsertVideo_Tag(Guid videoID, Guid? TagID)
        {
            if (videoID !=null && TagID != null)
            {
                Video_TagDAO DAO = new Video_TagDAO();
                Video_Tag NewItem = new Video_Tag();
                NewItem.ID = Guid.NewGuid();
                NewItem.IDVideo = videoID;
                NewItem.IDTag = (Guid)TagID;
                DAO.InsertVideo_Tag(NewItem);
            }
        }

        private Guid? InsertTagIfNotExist(string tagName)
        {
            TagDAO TDAO = new TagDAO();
            return TDAO.InsertAndReturnID(tagName);
        }


        public void AddTagOfVideo(Guid videoID, string tagString)
        {
            
            // Split string to
            string[] Tags = tagString.Split(';');
                        
            foreach (string tag in Tags)
            {
                if (tag != string.Empty)
                {
                    // Insert Tag if not exist
                    Guid? TagID = InsertTagIfNotExist(tag);
                    // Insert Video_Tag for this video
                    InsertVideo_Tag(videoID, TagID);
                }
            }
        }
    }
}
